<?php
class FileRepository {
	
	var $baseDir = null;
	var $shiftTime = 240; // hours, 10 days until I'll be have 5 net storages
						  // decrease to 3 hours
	var $nsf;
	
	public function __construct($baseDir=null) {
		$this->baseDir = $baseDir;
		
		import("net.storage.NetStorageFactory");
		import("io.File");
		
		$this->nsf = new NetStorageFactory();
		$this->initStorages();
		
		$this->clear();
	}
	
	protected function initStorages() {
		$this->nsf->registryStorage("test", "net.storage.StorageServiceFileupyours", "igormelnik","access");		
	}
	
	function setBaseDir($path) {
		$this->baseDir = $path;
	}
	function getBaseDir() {
		return $this->baseDir;
	}
	
	function setShiftTime($time) {
		$this->shiftTime = $time;
	}
	function getShiftTime() {
		return $this->shiftTime;
	}
	
	function getContent($fileName) {
		$file = new File($this->baseDir."/".$fileName);
		return $this->nsf->getFileContent($file);
	}
	
	function clear() {
		if ($this->baseDir!=null) {
			
		    // create a handler for the directory
		    $handler = opendir($this->baseDir);
		
		    // keep going until all files in directory have been read
		    while ($file = readdir($handler)) {
		        if ($file != '.' && $file != '..' && !(is_dir($file))) {
		        	$fdate = filectime($this->baseDir."/".$file);
		        	if ($fdate < (time()-($this->shiftTime*60*60)) ) {
		        		unlink($this->baseDir."/".$file);
		        	}
		        }
		    }
		
		    // tidy up: close the handler
		    closedir($handler);
		}
	}
}
?>